const query = require('../mysql');      //这个是封装的查询数据库的方法
//查询获取所有小说列表
async function getXiaoshuoList(ctx){
    let {body} =ctx.request 
    let pageNo = Number(body.page) || 1;
    let pageCount = Number(body.pageSize) || 10;
    let pageSize = pageCount;
    let page = (pageNo - 1) * pageCount;
    console.log(page,pageSize);
    let sqlStr = `select * from xiaoshuo limit ${page},${pageSize}`;//从第几条开始取，取几条
    let list = await query(sqlStr);
    let json = {
        success:true,
        data:list,
		query:body
    };
    ctx.response.body = json;
}

//新增笔记
async function addXiaoshuoListasync(ctx){
    let {body} =ctx.request 
    let sql ="INSERT INTO xiaoshuo (content) VALUES(?)"
    var queryParams=[body.insertText]
    var res= await query(sql,queryParams)
    let sql2 = 'select * from xiaoshuo';
    let list = await query(sql2);
    let json = {
        success:true,
        data:{
            code:10000,
            data:list
        },
		query:queryParams
    };
    ctx.response.body = json;
}
//查询相关的笔记
async function searchXiaoshuoList(ctx){
    let resParams = ctx.query;
    console.log("查询的内容：",resParams.searchContent)
    let sql = `select * from xiaoshuo where content like '%${resParams.searchContent}%'`;
    let list = await query(sql);
    let json = {
        success:true,
        data:list,
		query:resParams
    };
    ctx.response.body = json;
}
module.exports={
    getXiaoshuoList,
    addXiaoshuoListasync,
    searchXiaoshuoList
}